<template>
  <div id="app-container" style="margin: 10px">
    <!--条件查询表单-->
    <el-form :inline="true" class="demo-form-inline" style="margin-bottom: -10px">
      <el-form-item label="用户ID">
        <el-input v-model="queryUserTrainingDTO.userId" placeholder="用户ID" size="small" clearable/>
      </el-form-item>
      <el-form-item label="靶场ID">
        <el-input v-model="queryUserTrainingDTO.trainingId" placeholder="靶场ID" size="small" clearable/>
      </el-form-item>
      <el-form-item label="解析状态">
        <el-select
          v-model="queryUserTrainingDTO.processStatus"
          placeholder="解析状态"
          style="width: 120px;"
          clearable
          size="small">
          <el-option label="未使用解析" value="0"/>
          <el-option label="已使用解析" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="通过状态">
        <el-select
          v-model="queryUserTrainingDTO.passStatus"
          placeholder="通过状态"
          style="   width: 120px;"
          clearable
          size="small">
          <el-option label="未通过" value="0"/>
          <el-option label="已通过" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="queryUserTrainingDTO.createTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          clearable
          size="small"/>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryUserTrainingDTO.createTimeEnd"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          clearable
          size="small"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" size="small" @click="UserTrainingPageListQuery()">查询</el-button>
      <el-button plain icon="el-icon-refresh" size="small" style="margin-left: 0" @click="resetData()">清空</el-button>
    </el-form>
    <el-form :inline="true">
      <router-link to="/userTraining/add">
        <el-button type="primary" plain icon="el-icon-plus" size="small">新增数据</el-button>
      </router-link>
      <el-button type="danger" plain icon="el-icon-plus" size="small" @click="deleteBySelect">批量删除</el-button>
      <el-button type="warning" plain icon="el-icon-delete" size="small" style="margin-left: 0" @click="handleExport">导出数据</el-button>
    </el-form>

    <!--数据显示列表-->
    <el-table
      :data="userTrainingList"
      fit
      highlight-current-row
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
        align="center"/>
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ (currentPage-1)*pageSize + scope.$index +1 }}
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="用户名称" width="120" align="center"/>
      <el-table-column prop="trainingName" label="靶场名称" width="120" align="center"/>
      <el-table-column label="解析状态" prop="processStatus" width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.processStatus == '0'">未使用解析</span>
          <span v-if="scope.row.processStatus == '1'">已使用解析</span>
        </template>
      </el-table-column>
      <el-table-column label="通过状态" prop="passStatus" width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.passStatus == '0'">未通过</span>
          <span v-if="scope.row.passStatus == '1'">已通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="score" label="分数" width="160" align="center"/>
      <el-table-column prop="createTime" label="添加时间" width="500" align="center"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <router-link :to="'/userTraining/update/'+scope.row.id">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini">编辑</el-button>
          </router-link>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import userTraining from '../../api/userTraining'
import fileDownload from 'js-file-download'
import axios from 'axios'
export default {
  data() {
    return {
      queryUserTrainingDTO: {
        userId: '',
        processStatus: '',
        passStatus: '',
        createTimeStart: '',
        createTImeEnd: ''
      },
      userTrainingList: [],
      currentPage: 1,
      pageSize: 10,
      total: 1,
      ids: [],
      multipleSelection: ['']
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    UserTrainingPageListQuery() {
      this.initDate()
    },
    resetData() {
      this.queryUserTrainingDTO = {}
      this.initDate()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initDate()
    },
    handleDelete(id) {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        this.ids.push(id)
        userTraining.deleteUserTraining(this.ids).then(response => {
          this.$message.success('删除成功')
          this.initDate()
          this.ids = []
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = []
      for (let i = 0; i < val.length; i++) {
        if (this.multipleSelection.indexOf(val[i].id) === -1) {
          this.multipleSelection.push(val[i].id)
        }
      }
      console.log(this.multipleSelection)
    },
    deleteBySelect() {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        userTraining.deleteUserTraining(this.multipleSelection).then(response => {
          this.$message.success('删除成功')
          this.multipleSelection = []
          this.initDate()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    initDate() {
      userTraining.listPage(this.currentPage, this.pageSize, this.queryUserTrainingDTO).then(response => {
        this.userTrainingList = response.data.data
        this.total = response.data.total
      })
    },
    handleExport() {
      console.log('===导出数据===')
      axios({
        method: 'post',
        url: 'http://localhost:8080/userTraining/export',
        responseType: 'blob',
        data: this.queryUserTrainingDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '靶场用户信息.xlsx')
        }
      })
    }
  }
}
</script>
